﻿using AviewMotionUI.MachineLife;
using AviewMotionUI.ProductionStatistics;
using FinalTestMachine.Connect;
using FinalTestMachine.Motion.Process;
using FinalTestMachine.Motion.Process.SubProcess;
using FinalTestMachine.View;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Services.Description;
using System.Windows;
using WpfZMotion_AAControl.Core.Tools;

namespace FinalTestMachine.Motion
{
    /// <summary>
    /// 静态初始化类
    /// 来初始化控制器，所有轴对象的创建、参数的读取、外部设备的链接
    /// </summary>
    public static class StaticInitial
    {
        /// <summary>
        /// 这里用来创建所有对象
        /// </summary>
        static StaticInitial()
        {
            //创建RM_2
            StandardOrder = new RM_dll2.StandardOrderHelper(StaticAixInfoRegisterManager.aixRegisterInfo);
            Motion = new RM_dll2.MotionHelper(StandardOrder);

            //创建所有轴对象
            Aixs.Aix_Turn.CreateInstrance();
            Aixs.Site2.Aix_Site2_PlaneSourceLift.CreateInstrance();
            Aixs.Site2.Aix_Site2_RelayMirror.CreateInstance();
            Aixs.Site4.Aix_Site4_PlaneSourceLift.CreateInstrance();
            Aixs.Site4.Aix_Site4_RelayMirror.CreateInstance();
            Aixs.Site6.Aix_Site6_PipeUpDown.CreateInstrance();

            PipeController.CreateInstance();

            //创建其它参数对象
            OtherParamHelper.CreateInstrance();
            ProductionStatisticsManger.CreateInstance();

            Funs.FunCommSelection.CreateInstrance();
            Funs.FunStationSettings.CreateInstrance();

            //创建所有测试站对象
            FirstTestStation.CreateInstance();
            SecondTestStation.CreateInstance();
            ThirdTestStation.CreateInstance();
            FourTestStation.CreateInstance();
            FifthTestStation.CreateInstance();
            SixthTestStation.CreateInstance();
        }

        #region 对象

        public static readonly RM_dll2.MotionHelper Motion;
        public static readonly RM_dll2.StandardOrderHelper StandardOrder;

        /// <summary>
        /// 离焦测试
        /// </summary>
        public static RM_dll2.Process.IActionTrigger DefocusTest;

        #endregion 对象

        public static readonly RM_dll2.UPHHelper uPHHelper = new RM_dll2.UPHHelper(() => Products.ProductNumberManager.Cur.OKNums);

        public static void Initial()
        {
           

            ZMotionHelper.SingleAix.SingleAixHelper.IsCheckOrgStateOnHoming = false;

            //读取所有参数
            ISaveReaddll.SaveReadManagerHelper.Fun_ReadAll();

            //设置RM复位、自动运行对象
            Motion.SetProcessResMachine(Process.Process_ResMachine.Cur);
            Motion.SetProcessAutoRun(Process.Process_AutoRun.Cur);

            uPHHelper.Register(Process.Process_AutoRun.Cur);
            //初始化RM
            Motion.Initial();

            //初始化IO
            IO.StaticIOHelper.Initial();

            //初始化其它
            ConnectFactory.Initial();

            AM.MESAdapter.MESHelper.Cur.SetMESParameters(Motion, ConnectFactory.tcpClientMes, () => ConnectFactory.tcpClientMes.IsConnected, () => OtherParamHelper.Cur.MesStationName.GetValue, () => WorkItemManagerHelper.LoadedName);

            //给站位通讯管理类进行实例化，加载进程序内部
            SiteConnectManager.CreateInstance();
        }

        public static void Close()
        {
            void SafeAction(params Action[] actions)
            {
                foreach (var item in actions)
                {
                    try
                    {
                        item();
                    }
                    catch
                    {
                    }
                }
            }

            SafeAction(Motion.Close,
            IO.StaticIOHelper.Close,
            ConnectFactory.Close,
            CardInital.Close
            );
        }
    }
}